package by.home.controller;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import java.util.StringTokenizer;

public class SimpleActionManager extends ActionManager {

  ApplicationContext applicationContext = new FileSystemXmlApplicationContext(new String[]{"//home/alexey/SamSolutions/hello-webapp/hello-webapp/chat-simple/src/main/resources/beans.xml"});

  public SimpleActionManager() {
    super(null);
  }

  @Override
  public Action getActionByPageName(String page) {
    Action action;
    String pageName = getRequestPageByName(page);
    action = (Action) applicationContext.getBean(pageName);
    return action;

  }

  private String getRequestPageByName(String page) {
    StringTokenizer stringTokenizer = new StringTokenizer(page, "/");
    String requestPage = "";
    while (stringTokenizer.hasMoreTokens()) {
      requestPage = stringTokenizer.nextToken();
    }
    return requestPage;
  }
}